package com.cac.demo.controller.msBulkyItems;

import com.alibaba.fastjson.JSON;
import com.cac.demo.service.msBulkyItems.MsBulkyItemsService;
import com.cac.demo.util.adapter.*;
import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import static com.cac.demo.util.adapter.BusinessUtil.getSortExpColumnName;


@Slf4j
@RestController
@RequestMapping("/msBulkyItems")
public class MsBulkyItemsRest {
    @Autowired
    private MsBulkyItemsService msBulkyItemsService;
    /*大件货物明细查询*/
    @PostMapping("/msBulkyItemsSearch")
    public ResponseMsg<QueryRespBean<HashMap>> msBulkyItemsSearch(@RequestBody Map map){
        ResponseMsg<QueryRespBean<HashMap>> responseMsg = new ResponseMsg<>();
        //分页
        String param = JSON.toJSONString(map.get("pageParameter"));
        PageParameter pageParameter = JSON.parseObject(param, PageParameter.class);
        //查询条件
        String searchParamsParams = JSON.toJSONString(map.get("searchParams"));
        HashMap searchParams = JSON.parseObject(searchParamsParams, HashMap.class);

        QueryReqBean<HashMap> queryReqBean = new QueryReqBean<>();
        queryReqBean.setSearchParams(searchParams);
        queryReqBean.setPageParameter(pageParameter);

        if (StringUtils.hasText(queryReqBean.getSidx()) && StringUtils.hasText(queryReqBean.getSord())) {
            String sordExp = getSortExpColumnName(queryReqBean.getSidx(), queryReqBean.getSord(), HashMap.class);
            if (StringUtils.hasText(sordExp)) {
                queryReqBean.setSortExp(sordExp);
            }
        }
        if (StringUtils.hasText(queryReqBean.getKeyWord())) {
            DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            HashMap searchKeyWordParam = JsonHelper.getInstance().readValue(queryReqBean.getKeyWord(), dateFormat, new TypeReference<HashMap>() {
            });
            queryReqBean.setSearchParams(searchKeyWordParam);
        }

        QueryRespBean<HashMap> result = msBulkyItemsService.msBulkyItemsSearch(queryReqBean, ContextCommonHolder.getOrgId(),queryReqBean.getSortExp());
        List<HashMap> result1 = msBulkyItemsService.msBulkyItemsSearch(searchParams, ContextCommonHolder.getOrgId(),queryReqBean.getSortExp());
        if(result1.size()==0){
            responseMsg.setRetCode("201").setMessage("未查询到数据").setResponseBody(result);
        }
        else responseMsg.setResponseBody(result);
        return  responseMsg;

    }

    /*大件货物明细新增*/
    @PostMapping("/msBulkyItemsInsert")
    public ResponseMsg<Map> msBulkyItemsInsert(@RequestBody HashMap map){
        ResponseMsg<Map> responseMsg =  new ResponseMsg<>();
        String IP = null;
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        if (request.getHeader("x-forwarded-for") == null) {
            IP = request.getRemoteAddr();
        } else {
            IP = request.getHeader("x-forwarded-for");
        }
        log.info("IP" + IP);
        String userCode =  (String)request.getSession().getAttribute("account");
        if (Objects.isNull(userCode)) {
            log.info("session过期，请重新登陆");
            responseMsg.setMessage("账户已过期，请重新登录").setRetCode("201");
            return responseMsg;
        }
        int rsl=msBulkyItemsService.searchItemCode(map);
        if(rsl>0){
            return responseMsg.setMessage("该图号已经存在！").setRetCode("201");
        }
        map.put("IP",IP);
        map.put("userCode",userCode);
        Map reMsg = new HashMap();
        int s = 0;
        int f = 0;
        try {
            s+=msBulkyItemsService.msBulkyItemsInsert(map);
        }catch(Exception e)
        {
            e.printStackTrace();
            f++;
        }
        reMsg.put("成功：", s);
        reMsg.put("失败：", f);
        if (s == 0 && f == 0) {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("新增失败！");
        }
        if (s == 0 && f != 0) {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("新增失败！");
        }
        if (s != 0 && f == 0) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("新增成功！");
        }
        return responseMsg;
    }
    /*大件货物明细修改*/
    @PostMapping("/msBulkyItemsUpdate")
    public ResponseMsg<Map> msBulkyItemsUpdate(@RequestBody HashMap map){
        ResponseMsg<Map> responseMsg = new ResponseMsg<>();
        Map reMsg = new HashMap();
        int s = 0;
        int f = 0;
        int rsl=msBulkyItemsService.searchItemCode(map);
        if(rsl>0){
            return responseMsg.setMessage("该图号已经存在！").setRetCode("201");
        }
        if(map.get("msBulkyItemsId")!= null){
            try {
                msBulkyItemsService.msBulkyItemsUpdate(map);
                s++;
            }catch(Exception e)
            {
                e.printStackTrace();
                f++;
            }
        }
        else return responseMsg.setRetCode("201").setMessage("请选中一条数据！");
        reMsg.put("成功：", s);
        reMsg.put("失败：", f);
        if (s == 0 && f != 0) {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("修改失败！");

        }
        if (s != 0 && f == 0) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("修改成功！");
        }
        return responseMsg;
    }
    /*大件货物明细删除*/
    @PostMapping("/msBulkyItemsDelete")
    public ResponseMsg<Map> msBulkyItemsDelete(@RequestBody HashMap map) {
        ResponseMsg<Map> responseMsg = new ResponseMsg<>();
        Map reMsg = new HashMap();
        int s = 0;
        int f = 0;
        List<String> ids = (List<String>) map.get("msBulkyItemsId");
        if(ids.size()==0){
            return responseMsg.setRetCode("201").setMessage("请选择需要删除的数据");
        }
        for(int i = 0 ;i<ids.size();i++){
            String msBulkyItemsId = ids.get(i);
            if(msBulkyItemsId!=null){
                msBulkyItemsService.msBulkyItemsDelete(msBulkyItemsId);
                s++;
            }
            else f++;
        }
        reMsg.put("成功：", s);
        reMsg.put("失败：", f);
        if (s == 0 && f != 0) {
            responseMsg.setRetCode("201");
            responseMsg.setMessage("删除失败！");

        }
        if (s != 0 && f == 0) {
            responseMsg.setRetCode("200");
            responseMsg.setMessage("删除成功！");
        }
        if (s != 0 && f != 0) {
            responseMsg.setRetCode("202");
            responseMsg.setMessage("删除有失败项！");
            responseMsg.setResponseBody(reMsg);
        }
        return responseMsg;
    }
}
